package com.epoint.androidphone.mobileoa.ui.sysconfig;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;
import com.epoint.androidmobile.bizlogic.dbservice.ConfigKey;
import com.epoint.androidmobile.bizlogic.setting.model.SettingListModel;
import com.epoint.androidmobile.bizlogic.setting.task.ConnectionTestTask;
import com.epoint.androidmobile.bizlogic.update.task.UpdateTask;
import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.phoneutil.PhoneHelp;
import com.epoint.androidmobile.core.string.StringHelp;

/**
 * @author liyc
 * @time 2013-5-7 上午9:01:42
 * @annomation 系统设置
 */
public class SysConfigView extends SuperPhonePage {
	long updateClientTaskId,testConnctionTaskId;
	private List<SettingListModel> slist = new ArrayList<SettingListModel>();
	ListView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.sysconfig, "系统设置");
		lv = (ListView) findViewById(R.id.lv);
		lv.setOnItemClickListener(this);
		refreshListView();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		super.onItemClick(parent, view, position, id);
		slist.get(position).executeOnClickListener(view);
	}

	private void refreshListView()
	{
		slist.clear();

		SettingListModel slm = new SettingListModel();
		slm.title = "服务器设置";
		slm.info = dbUtil.getConfigValue(ConfigKey.webserviceurl);
		slm.icon_left = R.drawable.spanner_64;
		slm.img_right = R.drawable.img_sys_lvright1;
		slm.setOnClickListener(new SettingListModel.OnClickListener() {

			@Override
			public void onClick(View v) {
				LayoutInflater inflater2 = LayoutInflater.from(getContext());
				LinearLayout ll = (LinearLayout) inflater2.inflate(R.layout.alertinput_editview, null);
				final EditText et = (EditText) ll.findViewById(R.id.ai_et);
				et.setText(dbUtil.getConfigValue(ConfigKey.webserviceurl));
				new AlertDialog.Builder(getContext())
				.setTitle("服务器设置")
				.setView(ll)
				.setPositiveButton("保存", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dbUtil.setConfigValue(ConfigKey.webserviceurl, et.getText().toString());
						refreshListView();
						AlertUtil.Hide(dialog);
					}
				})
				.setNegativeButton("取消", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						AlertUtil.Hide(dialog);
					}
				})
				.setNeutralButton("测试", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						HashMap<String, Object> params = getPassMap();
						params.put("newURL", et.getText().toString());
						testConnctionTaskId = new ConnectionTestTask(SysConfigView.this, params).startTask();
						AlertUtil.Keep(dialog);
					}
				})
				.create()
				.show();
			}
		});
		slist.add(slm);

		slm = new SettingListModel();
		slm.title = "自动登录";

		String autologin = dbUtil.getConfigValue(ConfigKey.autologin);
		if(autologin.equals("1")){
			slm.info = "自动登陆";
			slm.img_right = R.drawable.bg_settings_drag_on;
		}
		else{
			slm.info = "自动登陆已取消";
			slm.img_right = R.drawable.bg_settings_drag_off;
		}

		slm.icon_left = R.drawable.key_64;
		slm.setOnClickListener(new SettingListModel.OnClickListener() {
			@Override
			public void onClick(View v) {
				//自动登录
				String autologin = dbUtil.getConfigValue(ConfigKey.autologin);
				if(autologin.equals("1")){
					dbUtil.setConfigValue(ConfigKey.autologin, "0");
				}
				else{
					dbUtil.setConfigValue(ConfigKey.autologin, "1");
				}
				refreshListView();
			}

		});
		slist.add(slm);

		slm = new SettingListModel();
		slm.title = "全屏切换";
		String isfullscreen_db = dbUtil.getConfigValue(ConfigKey.isFullScreen);
		if(isfullscreen_db.equals("1")){
			slm.info = "当前全屏";
			slm.img_right = R.drawable.bg_settings_drag_on;
		}
		else{
			slm.info = "当前非全屏";
			slm.img_right = R.drawable.bg_settings_drag_off;
		}
		slm.icon_left = R.drawable.resize_64;
		slm.setOnClickListener(new SettingListModel.OnClickListener() {
			@Override
			public void onClick(View v) {
				//全屏切换

				String isfullscreen_db = dbUtil.getConfigValue(ConfigKey.isFullScreen);
				if(isfullscreen_db.equals("1")){
					dbUtil.setConfigValue(ConfigKey.isFullScreen, "0");
				}
				else{
					dbUtil.setConfigValue(ConfigKey.isFullScreen, "1");
				}
				setScreenMode();
				refreshListView();
			}

		});
		slist.add(slm);

		slm = new SettingListModel();
		slm.title = "软件更新";
		slm.info = PhoneHelp.getVersionName(this);
		slm.icon_left = R.drawable.reload_64;
		slm.img_right = R.drawable.img_sys_lvright1;
		slm.setOnClickListener(new SettingListModel.OnClickListener() {
			@Override
			public void onClick(View v) {
				showTopProgressBar();
				HashMap<String, Object> params = getPassMap();
				params.put("updatepath", InitView.updatenPath);
				updateClientTaskId = new UpdateTask(SysConfigView.this,params).startTask();
			}

		});
		slist.add(slm);
		MyAdapter adapter = new MyAdapter();
		lv.setAdapter(adapter);
	}

	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==updateClientTaskId)
		{
			boolean hasUpdate = (Boolean) obj;
			if(!hasUpdate)
			{
				ToastUtil.toastLong(this, "当前已经是最新版本");
			}
		}
	}
	
	

	public class ViewHolder {
		public ImageView ivLeft;
		public ImageView ivRight;
		public TextView tvTop;
		public TextView tvBottom;
	}

	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return slist.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater.from(getContext());
				convertView = mInflater.inflate(R.layout.lv_setting, null);
				holder.ivLeft = (ImageView) convertView
						.findViewById(R.id.ivIconLeft);
				holder.ivRight = (ImageView) convertView
						.findViewById(R.id.ivImgRight);
				holder.tvTop = (TextView) convertView
						.findViewById(R.id.tvTitle);
				holder.tvBottom = (TextView) convertView
						.findViewById(R.id.tvInfo);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			SettingListModel slm = slist.get(position);

			holder.ivLeft.setImageResource(slm.icon_left);
			//holder.ivLeft.setVisibility(View.GONE);
			holder.ivRight.setImageResource(slm.img_right);
			holder.tvTop.setText(slm.title);

			holder.tvBottom.setText(slm.info);
			return convertView;
		}

	}
}
